package cn.edu.lingnan.service.impl;

import cn.edu.lingnan.domain.Dto.UserLoginDto;
import cn.edu.lingnan.domain.Dto.UserRegisterDto;
import cn.edu.lingnan.domain.pojo.Permission;
import cn.edu.lingnan.domain.pojo.User;
import cn.edu.lingnan.mapper.UserMapper;
import cn.edu.lingnan.service.UserService;
import cn.edu.lingnan.utils.EmailUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl  implements UserService {

    @Autowired
    private UserMapper userMapper;
    @Override
    public User login(UserLoginDto userLoginDto) {
        User user = userMapper.login(userLoginDto );
        if (user == null) {
            return null;
        }
        List<Permission> permissions=userMapper.getPermissions(user.getRoleId());
        user.setPermissions(permissions);
        return   user;
    }

    @Override
    public Integer register(UserRegisterDto userRegisterDto, String sessionId) {
        boolean verified = EmailUtil.verifyCaptcha(userRegisterDto.getEmail(), userRegisterDto.getCaptcha(), sessionId);
        if (!verified) {
             return -1;
        }
        userMapper.register(userRegisterDto);


        return 1;
    }
}
